import socketio

# 实例化
sio = socketio.Client()


# 默认连接时执行
@sio.event
def connect():
    print('connection established')


# 监听事件
@sio.event
def my_message(data):
    print('message received with ', data)
    # 发送事件sio.emit(对方事件名,data)
    sio.emit('my response', {'response': 'my response'})


# 自定义:监听事件
# @sio.on('event_to_client', namespace=['/receMess'])
# @sio.on('event_to_client', namespace='/receMess')
@sio.on('event_to_client')
def event_to_client(data):
    print('/////')
    print('message from web ', data)
    # return 'ok'


# 默认断开时执行
@sio.event
def disconnect():
    print('disconnected from server')
    pass


# 连接
# 只能连接一个，保证和Server的port一致
# sio.connect('http://127.0.0.1:8000')
sio.connect('http://127.0.0.1:5000', namespaces='/receMess')
sio.wait()